package com.easonji.attendance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.easonji.attendance.mapper.LeaveMapper;
import com.easonji.attendance.mapper.LeaveStateMapper;
import com.easonji.attendance.model.Leave;
import com.easonji.attendance.model.LeaveState;
import com.easonji.attendance.service.ILeaveStateService;
import com.easonji.common.base.query.BaseQuery;
import com.easonji.common.base.service.AbstractBaseService;
import com.easonji.common.factory.PageFactory;
import com.easonji.common.page.PageInfo;
import com.easonji.common.util.JsonUtils;
import com.easonji.common.util.StringUtils;
import com.easonji.core.exception.BizException;
import com.easonji.core.shiro.ShiroUser;
import com.easonji.core.shiro.util.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 请假状态 服务实现类
 * </p>
 *
 * @author easonji
 * @since 2018-10-04
 */
@Service
public class LeaveStateServiceImpl extends AbstractBaseService<LeaveState> implements ILeaveStateService {

    @Resource
    private LeaveStateMapper leaveStateMapper;

    @Resource
    private LeaveMapper leaveMapper;

    @Override
    protected JSONObject warp2Json(LeaveState leaveState) {
       JSONObject result = JsonUtils.parseObject(leaveState);

       return result;
    }


    @Override
    public Object queryList(BaseQuery baseQuery){

        // 分页信息
        Page<LeaveState> page = new PageFactory<LeaveState>().defaultPage();
        // 查询条件
        Wrapper<LeaveState> wrapper = new EntityWrapper<>();

        List<LeaveState> leaveStates = leaveStateMapper.selectPage(page, wrapper);

        return new PageInfo(warp(leaveStates), page.getTotal());
    }

    @Override
    public Object queryRowById(Integer lesId) {
        LeaveState leaveState = leaveStateMapper.selectById(lesId);
        return warp(leaveState);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = BizException.class)
    public Integer save(LeaveState leaveState) {

        ShiroUser shiroUser = ShiroUtils.getUser();

        leaveState.setLesDatetime(new Date());

        LeaveState leaveState_old = new LeaveState();
        BeanUtils.copyProperties(leaveState,leaveState_old);

        Integer insertint = leaveStateMapper.updateById(leaveState);

        if(insertint==0){
            return 0;
        }

        Integer otsState = 0;

        if(leaveState.getLesState() == 1 && leaveState.getLesPost()== 2){
            otsState = 1;
        }else if(leaveState.getLesState() == -1 && leaveState.getLesPost()== 2){
            otsState = -1;
        }else if(leaveState.getLesState() == 1 && leaveState.getLesPost()== 1){
            otsState = 2;
        }else if(leaveState.getLesState() == -1 && leaveState.getLesPost()== 1){
            otsState = -2;
        }


        LeaveState leaveState1 = queryLeaveStateasc(leaveState.getLesLeave());

        Leave leave = new Leave();
        Leave leaveForm = new Leave();
        leaveForm.setLeState(otsState);
        leaveForm.setLeUpdateUser(shiroUser.getUserId());
        leaveForm.setLeUpdateDate(new Date());
        leaveForm.setLeId(leaveState.getLesLeave());
        if(StringUtils.isNotEmpty(leaveState1)){
            leaveForm.setLeNextAuditor(leaveState1.getLesAuditor());
        }
        if(leaveState.getLesPost() ==1){
            leaveForm.setLeNextAuditor(0);
        }
        BeanUtils.copyProperties(leaveForm,leave);

        return leaveMapper.updateById(leave);
    }

    @Override
    public Integer update(LeaveState leaveStateForm) {
        LeaveState leaveState = new LeaveState();
        BeanUtils.copyProperties(leaveStateForm,leaveState);
        return leaveStateMapper.updateById(leaveState);
    }

    @Override
    public Integer delete(Integer lesId) {
        Wrapper<LeaveState> wrapper = new EntityWrapper<>();
        wrapper.eq("les_id", lesId);
        return leaveStateMapper.delete(wrapper);
    }

    @Override
    public List<Integer> queryLeaveIdByauditor(Integer auditor){


        Wrapper<LeaveState> wrapper = new EntityWrapper<>();
        wrapper.ne("les_state",0);
        wrapper.eq("les_auditor",auditor);
        wrapper.groupBy("les_leave");
        List<LeaveState> leaveStates = leaveStateMapper.selectList( wrapper);

        List<Integer> leaves = new ArrayList<>();
        for (LeaveState le :leaveStates){
            leaves.add(le.getLesLeave());
        }

        return leaves;
    }

    @Override
    public LeaveState queryLeaveStateasc(Integer oId){

        LeaveState leaveState = new LeaveState();

        Wrapper<LeaveState> wrapper = new EntityWrapper<>();

        wrapper.eq("les_leave",oId);

        wrapper.eq("les_state",0);

        wrapper.orderBy("les_id");

        List<LeaveState> leaveStates = leaveStateMapper.selectList( wrapper);

        if(leaveStates.size()>0){
            leaveState = leaveStates.get(0);
        }

        return leaveState;
    }

}
